/*
 * parserinterface.c
 *
 *  Created on: 2013-5-15
 *      Author: xkaying
 */

#include "parserinterface.h"

/*
 * parserenum.c
 *
 *  Created on: 2013-5-14
 *      Author: xkaying
 */

#include "parserenum.h"

#include "lexer.h"
#include "parsertemplate.h"
#include "../typedef/token.h"
#include "../typedef/package.h"
#include "../typedef/import.h"
#include "../typedef/interface.h"
#include "../common/strutils.h"

#include <assert.h>

#include "parser.h"
//枚举变量的解析

int ParserInterface(Lexer *lexer, Package *package, int prority)
{
	assert(lexer);
	assert(package);
	int state;
	state = 0;
	ptoken = LexerGetToken(lexer);
	Interface *pface;
	pface = InterfaceInit(prority);
	while (ptoken) {
		switch (state)
		{
			case 0: {
				//名字
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Less: {
						//进入模板读取
						Template *ptmp;
						ptmp = ParserTemplate(lexer);
						if (ptmp) {
							if (!TemplateIsEmpty(ptmp))
								InterfaceAppendTemplate(pface, ptmp);
							state = 1;
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Variable: {
						InterfaceAppendName(pface, ptoken->name);
						state = 2;
						continue;
					}
					default: {
						printf("第%d行:接口类型定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						InterfaceAppendName(pface, ptoken->name);
						state = 2;
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:接口类型定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_BlockStart: {
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//表示枚举类型定义结束,定义了一个空接口
					case TokenType_End: {
						printf("第%d行:警告 定义了一个空接口类型.\n", ptoken->line);
						PackageAppendInterface(package, pface);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
						//读取继承父接口
					case TokenType_Extend: {
						state = 20;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:接口类型定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser enum状态机错误.状态%d.\n", state);
				return 0;
			}
			case 20: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 21;
						InterfaceAppendSuper(pface, ptoken->name);
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						printf("第%d行:警告 '{'前面多了个':'.(是否漏写了父接口)\n");
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:接口类型定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 21: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Comma: {
						state = 22;
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:接口类型定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 22: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 21;
						InterfaceAppendSuper(pface, ptoken->name);
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						printf("第%d行:警告 '{'前面多了个',',该符号被忽视.\n");
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:接口类型定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_BlockEnd: {
						printf("第%d行:警告 定义了一个空接口类型.\n", ptoken->line);
						PackageAppendInterface(package, pface);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
					case TokenType_End: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_OPERATOR: {
						Function *pfun;
						pfun = ParserFOperator(lexer, 0);
						if (pfun) {
							if (FunctionIsempty(pfun)) {
								printf("警告:定义在接口中操作符,不能拥有内容.自动忽略其内容.\n");
							}
							InterfaceAppendFunction(pface, pfun);
							state = 4;
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (FunctionIsempty(pfun)) {
								printf("警告:函数%s定义在接口中,不能拥有内容.自动忽略函数内容.\n",
										FunctionName(pface));
							}
							InterfaceAppendFunction(pface, pfun);
							state = 4;
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 接口类型内部接口语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 4: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_BlockEnd: {
						PackageAppendInterface(package, pface);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
					case TokenType_End: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_OPERATOR: {
						Function *pfun;
						pfun = ParserFOperator(lexer, 0);
						if (pfun) {
							if (FunctionIsempty(pfun)) {
								printf("警告:定义在接口中操作符,不能拥有内容.自动忽略其内容.\n");
							}
							InterfaceAppendFunction(pface, pfun);
							state = 4;
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (FunctionIsempty(pfun)) {
								printf("警告:函数%s定义在接口中,不能拥有内容.自动忽略函数内容.\n",
										FunctionName(pface));
							}
							InterfaceAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 接口类型内部接口语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			default: {
				printf("parser interface状态机错误.状态:default.\n");
				return 0;
			}
		}
	}
	printf("不完整的接口类型,是否缺少}结尾.\n");
	return 0;
}

